import 'package:edu/constants/app_strings.dart';
import 'package:edu/pages/course/course_detail/course_detail_page_logic.dart';
import 'package:edu/pages/course/course_detail/video_player/course_video_player_widget.dart';
import 'package:edu/pages/course/course_detail/video_player/course_video_player_widget_logic.dart';
import 'package:edu/utils/account_info.dart';
import 'package:edu/utils/app_router.dart';
import 'package:edu/utils/app_theme.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

class CourseDetailBottomBar extends StatelessWidget {
  final GlobalKey purchaseButtonKey;
  final Function startAnimate;

  const CourseDetailBottomBar({
    super.key,
    required this.purchaseButtonKey,
    required this.startAnimate,
  });

  @override
  Widget build(BuildContext context) {
    return Selector<CourseDetailPageLogic, bool>(
      selector: (_, logic) => logic.isContentEmpty || logic.isMycourse,
      builder: (context, v, child) {
        if (v) {
          return const SizedBox.shrink();
        }
        return BottomAppBar(
          height: 50,
          padding: EdgeInsets.zero,
          child: Selector<CourseDetailPageLogic, bool>(
            // 课程是否已购
            selector: (_, logic) => logic.isPurchased,
            builder: (context, v, child) {
              final purchaseTextColor = AppTheme.shared.whiteColor;
              final purchaseTextStyle = Theme.of(context).textTheme.titleSmall
                  ?.copyWith(
                    color: purchaseTextColor,
                    fontWeight: FontWeight.bold,
                  );
              return Row(
                children: [
                  if (!v)
                    // 试看
                    InkWell(
                      onTap: () {
                        if (AccountInfo.shared.isLoggedIn == false) {
                          context.pushNamed(AppRouter.kLogin);
                          return;
                        }
                        final logic = context.read<CourseDetailPageLogic>();
                        if (logic.categoryUnits.length > 2) {
                          final unit = logic.categoryUnits[1];
                          showDialog(
                            context: context,
                            builder: (_) {
                              return ChangeNotifierProvider(
                                create: (_) =>
                                    CourseVideoPlayerWidgetLogic(id: unit.id),
                                child: const CourseVideoPlayerWidget(),
                              );
                            },
                          );
                        }
                      },
                      child: SizedBox(
                        width: 60,
                        child: Column(
                          children: [
                            const Spacer(),
                            const Icon(Icons.play_circle_outline, size: 25),
                            const SizedBox(height: 2),
                            Text(
                              AppStrings.freeWatch,
                              style: Theme.of(
                                context,
                              ).textTheme.bodySmall?.copyWith(fontSize: 12),
                            ),
                          ],
                        ),
                      ),
                    ),
                  SizedBox(width: v ? 16 : 10),
                  if (!v)
                    // 购物车
                    Expanded(
                      flex: 1,
                      child: Container(
                        margin: const EdgeInsets.symmetric(vertical: 7),
                        decoration: BoxDecoration(
                          color: AppTheme.shared.commonTipColor,
                          // 设置圆角
                          borderRadius: const BorderRadius.only(
                            topLeft: Radius.circular(30),
                            bottomLeft: Radius.circular(30),
                          ),
                        ),
                        child: Center(
                          child: GestureDetector(
                            key: purchaseButtonKey,
                            // 加入购物车，点击事件
                            onTap: () {
                              if (AccountInfo.shared.isLoggedIn == false) {
                                context.pushNamed(AppRouter.kLogin);
                                return;
                              }
                              final logic = context
                                  .read<CourseDetailPageLogic>();
                              logic.addToShoppingCarts().then((value) {
                                if (value) {
                                  startAnimate();
                                }
                              });
                            },
                            child: Text(
                              AppStrings.putToCart,
                              style: purchaseTextStyle,
                            ),
                          ),
                        ),
                      ),
                    )
                  else
                    // 去学习按钮
                    Expanded(
                      child: TextButton(
                        onPressed: () {
                          context.pushNamed(
                            AppRouter.kEduCourse,
                            queryParameters: {
                              'id': context
                                  .read<CourseDetailPageLogic>()
                                  .courseId
                                  .toString(),
                            },
                          );
                        },
                        style: TextButton.styleFrom(
                          backgroundColor: Theme.of(
                            context,
                          ).colorScheme.primary,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(30),
                          ),
                          padding: const EdgeInsets.symmetric(vertical: 7),
                        ),
                        child: Text(
                          AppStrings.goToLearn,
                          style: purchaseTextStyle,
                        ),
                      ),
                    ),
                  if (!v)
                    // 购买按钮
                    Expanded(
                      flex: 1,
                      child: Container(
                        margin: const EdgeInsets.symmetric(vertical: 7),
                        decoration: BoxDecoration(
                          color: AppTheme.shared.purchaseTipColor,
                          borderRadius: const BorderRadius.only(
                            topRight: Radius.circular(30),
                            bottomRight: Radius.circular(30),
                          ),
                        ),
                        child: Center(
                          child: GestureDetector(
                            onTap: () async {
                              if (AccountInfo.shared.isLoggedIn == false) {
                                context.pushNamed(AppRouter.kLogin);
                                return;
                              }
                              final logic = context
                                  .read<CourseDetailPageLogic>();
                              final price = logic.courseDetail!.price; // 课程价格
                              final confirm = await showDialog<bool>(
                                context: context,
                                builder: (context) => AlertDialog(
                                  title: const Text(AppStrings.confrimPurchase),
                                  content: Text(
                                    '${AppStrings.coursePrice}$price',
                                  ),
                                  actions: <Widget>[
                                    TextButton(
                                      onPressed: () =>
                                          Navigator.of(context).pop(false),
                                      child: const Text(AppStrings.cancel),
                                    ),
                                    TextButton(
                                      onPressed: () =>
                                          Navigator.of(context).pop(true),
                                      child: const Text(AppStrings.confirm),
                                    ),
                                  ],
                                ),
                              );
                              if (confirm == true) {
                                logic.purchase();
                              }
                            },
                            child: Text(
                              AppStrings.buyNow,
                              style: purchaseTextStyle,
                            ),
                          ),
                        ),
                      ),
                    ),
                  const SizedBox(width: 16),
                ],
              );
            },
          ),
        );
      },
    );
  }
}
